let id = 0

class Dep {
    constructor() {
        // watcher 收集
        this.subs = []
        // 唯一标识
        this.id = id++
    }
    // 依赖收集
    depend() {
        // 为 watcher 添加当前 dep
        Dep.target.addDep(this)
    }
    // 派发更新
    notify() {
        this.subs.forEach(watcher => {
            watcher.update()
        })
    }
    // 与 wtacher 互相依赖
    addSub(watcher) {
        this.subs.push(watcher)
    }
}

// 往 原型 添加一个 target，默认为空
Dep.target = null
// 栈结构，为计算属性做准备
const stack = []
export function pushTarget(watcher) {
    stack.push(watcher)
    Dep.target = watcher
}

export function popTarget() {
    stack.pop()
    Dep.target = stack[stack.length - 1]
}

export default Dep



